<template>
	<xn-form-container
		:title="formData.modified_time ? '编辑接口封装表' : '增加接口封装表'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="ai_key：" name="ai_key">
				<a-input
					placeholder="请输入内容"
					v-model:value="formData.ai_key"
					:readonly="!!formData.modified_time"
				></a-input>
			</a-form-item>
			<a-form-item label="function_id：" name="function_id">
				<a-input
					placeholder="请输入内容"
					v-model:value="formData.function_id"
					:readonly="!!formData.modified_time"
				></a-input>
			</a-form-item>
			<a-form-item label="function_res_type：" name="function_res_type">
				<a-input placeholder="请输入内容" v-model:value="formData.function_res_type"></a-input>
			</a-form-item>
			<a-form-item label="function_batch：" name="function_batch">
				<a-input placeholder="请输入内容" v-model:value="formData.function_batch"></a-input>
			</a-form-item>
			<a-form-item label="flow_interval_second：" name="flow_interval_second">
				<a-input placeholder="请输入内容" v-model:value="formData.flow_interval_second"></a-input>
			</a-form-item>
			<a-form-item label="flow_rate：" name="flow_rate">
				<a-input placeholder="请输入内容" v-model:value="formData.flow_rate"></a-input>
			</a-form-item>
			<a-form-item label="function_protocol：" name="function_protocol">
				<a-input placeholder="请输入内容" v-model:value="formData.function_protocol"></a-input>
			</a-form-item>
			<a-form-item label="function_method：" name="function_method">
				<a-input placeholder="请输入内容" v-model:value="formData.function_method"></a-input>
			</a-form-item>
			<a-form-item label="function_info：" name="function_info">
				<a-input placeholder="请输入内容" v-model:value="formData.function_info"></a-input>
			</a-form-item>

			<a-form-item label="状态：" name="valid">
				<a-select
					v-model:value="formData.valid"
					placeholder="请选择状态"
					:options="[
						{ label: '有效', value: 1 },
						{ label: '失效', value: 0 }
					]"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../customerInfo/api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})

	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}
	// 默认要校验的
	const formRules = {
		ai_key: [required('请输入ai_key')],
		function_id: [required('请输入function_id')],
		valid: [required('请选择状态')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				api
					.submitForm(
						{
							...formData.value,
							...(formData.value.id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
						},
						formData.value.modified_time
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
